﻿using System;
#if DEBUG
using System.Diagnostics;
#endif // #if DEBUG
using System.Net;
using System.Text;
using System.Xml;
using System.Collections.Generic;

using FRL.DebtChased.Extensions;
using FRL.DebtChased.Repository;
using FRL.DebtChased.Types;

#if WORKFLOWEDITOR
using FRL.DebtChased.WorkflowEditor;
#endif // #if WORKFLOWEDITOR

namespace FRL.DebtChased.WorkflowObjects
{

	/// <summary>
	/// Declaration of WorkflowAction_SendEmail class
	/// Workflow action for sending an email
	/// </summary>
	public class WorkflowAction_SendEmail : WorkflowAction_Base
	{

		/////////////////////////////////////////////////////////////
		// Enumerations

		/////////////////////////////////////////////////////////////
		// Constants

		public const string XmlElementName = "WorkflowAction_SendEmail";
		public const string BoingLabel = "EMAIL";

		/////////////////////////////////////////////////////////////
		// Attributes

		private Definitions.eCommTargetType m_Target = Definitions.eCommTargetType.NotSet;
		public Definitions.eCommTargetType Target
		{
			get { return m_Target; }
			set { m_Target = value; }
		}

		private CommBodyWrapper m_commBodyWrapper = new CommBodyWrapper();
		public CommBodyWrapper CommBody
		{
			get { return m_commBodyWrapper; }
		}

		private int m_iAttachmentLetterTypeId;
		public int AttachmentLetterTypeId
		{
			get { return m_iAttachmentLetterTypeId; }
			set { m_iAttachmentLetterTypeId = value; }
		}

		/////////////////////////////////////////////////////////////
		// Implementation

		protected override string _getElementName()
		{
			return XmlElementName;
		}

#if WORKFLOWEDITOR
		public override string GetBoingLabel()
		{
			return BoingLabel;
		}
#endif // #if WORKFLOWEDITOR

#if WORKFLOWEDITOR
		public override string[] GetBoingSubStrings()
		{

			// Declare return variable
			string[] Ftmp = new string[2];

			// Row 0, target and template
			StringBuilder sb = new StringBuilder();
			sb.AppendFormat("To {0}, using ",m_Target);
			if (m_commBodyWrapper.IsEmpty)
				sb.Append("[no body]");
			else
				sb.Append(CachedData.Singleton.GetCommBodyDescription(m_commBodyWrapper));
			Ftmp[0] = sb.ToString();

			// Row 1, attachments
			if (m_iAttachmentLetterTypeId == 0)
				Ftmp[1] = "[no attachment]";
			else
				Ftmp[1] = string.Format("Attachment: {0}",CachedData.Singleton.GetLetterTypeName(m_iAttachmentLetterTypeId));

			// Return variable
			return Ftmp;

		}
#endif // #if WORKFLOWEDITOR

		protected override void _writeParameters(XmlWriter w)
		{

			// Write members as parameters
			w.WriteAttributeString("target", ((int)m_Target).ToString());
			w.WriteAttributeString("attachment", m_iAttachmentLetterTypeId.ToString());

			// Write elements
			m_commBodyWrapper.Write(w,null);

		}

		protected override void _readParameters(XmlReader r)
		{

			// Read parameters
			m_Target = (Definitions.eCommTargetType)r.SafeReadIntAttribute("target", (int)Definitions.eCommTargetType.Debtor);
			m_iAttachmentLetterTypeId = r.SafeReadIntAttribute("attachment", 0);

			// Loop through elements
			if (!r.IsEmptyElement)
			{

				// Loop through elements
				while (r.Read())
				{

					// If this is an element...
					if (r.NodeType == XmlNodeType.Element)
					{

						// Compare names
						if (r.Name == CommBodyWrapper.ElementName)
							m_commBodyWrapper.Read(r);
						else
						{
#if DEBUG
							Debug.Assert(false);
#endif // #if DEBUG
							r.SkipXmlElement();
						}

					}
					else
						break;

				} // while-loop

			}

		}

		public override void GetObjectsUsed(List<int> iCommBodiesUsed, List<int> iPopupsUsed, List<int> iLetterTypeIds)
		{

			// Add the comm-body id
			iCommBodiesUsed.AddUniqueNonZeroValue(m_commBodyWrapper.CommBodyId);

		}

		public override string GetSummary(Workflow.eSummaryFormat SummaryFormat)
		{

			// Declare return variable
			string strFtmp = null;

			// Compare formats
			if (SummaryFormat == Workflow.eSummaryFormat.Audit)
			{
				strFtmp = string.Format("Email {0}",m_Target);
			}
			else if (SummaryFormat == Workflow.eSummaryFormat.Clockwork)
			{
				strFtmp = string.Format("Email {0}", m_Target);
			}
#if DEBUG
			else
				Debug.Assert(false);
#endif // #if DEBUG

			// Return variable
			return strFtmp;

		}

#if REPOSITORY
		public override List<AccountNote> Process(DebtChasedEntities ent, Account acc, AccountInvoice inv)
		{

			// Declare return variable
			List<AccountNote> Ftmp = null;

			// Return variable
			return Ftmp;

		}
#endif // #if REPOSITORY

	}

}
